package com.aibili.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.UserAnonymous;
import com.alibaba.fastjson.JSONObject;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;
import com.daoshun.common.HttpRequest;

@Service
@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
public class AnonymousService extends BaseService {

	/**
	 * 
	 * @Description 生成管理员的token
	 * @return
	 */
	public boolean getAdminToken() {
		String url = "https://a1.easemob.com/" + DsConstant.ORG_NAME + "/" + DsConstant.APP_NAME + "/token";
		String data = "{\"" + DsConstant.GRANT_TYPE + "\":\"" + DsConstant.CLIENT_CREDENTIALS
				+ "\",\"client_id\":\"" + DsConstant.CLIENT_ID + "\","
				+ "\"client_secret\":\""+ DsConstant.CLIENT_SECRET +"\"}";
		//System.out.println("data2:" + data);
		String backstr = HttpRequest.httpPostSend(url, data, false);

		JSONObject backjson = JSONObject.parseObject(backstr);
		String error = backjson.getString("error");
		if (error == null) {
			String access_token = backjson.getString("access_token");
			if (access_token != null) {
				DsConstant.ACCESS_TOKEN = access_token;
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @Description 注册
	 * @param anon_id
	 * @param anon_pass
	 * @param nick_name
	 * @return
	 */
	public String register(String anon_id, String anon_pass, String nick_name) {
		String backstr = null;
		String url = "https://a1.easemob.com/" + DsConstant.ORG_NAME + "/" + DsConstant.APP_NAME + "/users";
		String data = "{\"username\":\"" + anon_id + "\",\"password\":\"" + anon_pass + "\"";
		if (!CommonUtils.isEmptyString(nick_name)) {
			data += ",\"nick_name\":\"" + nick_name + "\"";
		}
		data += "}";
		backstr = HttpRequest.httpPostSend(url, data, true);
		JSONObject backjson = JSONObject.parseObject(backstr);
		String error = backjson.getString("error");
		if (error != null) {
			if (error.equals("duplicate_unique_property_exists")) { // 用户已注册

			} else if (error.equals("unauthorized")) { // 未有权限（从新获取权限）
				getAdminToken();
			}
		}
		return backstr;
	}

	/**
	 * 
	 * @Description 通过user_id 获得匿名信息
	 * @param user_id
	 * @return
	 */
	public UserAnonymous getByUserid(long user_id) {
		String hql = " from UserAnonymous where user_id = :user_id";
		UserAnonymous anonymous = (UserAnonymous) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id" },
				user_id);
		return anonymous;
	}

	/**
	 * 
	 * @Description 修改匿名信息
	 * @param anonymous
	 */
	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
	public void update(UserAnonymous anonymous) {
		dataDao.updateObject(anonymous);
	}

	/**
	 * 
	 * @Description 新增匿名信息
	 * @param anonymous
	 */
	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
	public void add(UserAnonymous anonymous) {
		dataDao.addObject(anonymous);
	}

	/**
	 * 
	 * @Description 删除环信id
	 * @param anonymous_id
	 */
	public void deleteAnonymous_id(String anonymous_id) {
		String backstr = null;
		String url = "https://a1.easemob.com/" + DsConstant.ORG_NAME + "/" + DsConstant.APP_NAME + "/users/"
				+ anonymous_id;
		backstr = HttpRequest.httpDeleteSend(url, "", true);
		//System.out.println(backstr);
	}

	/**
	 * 
	 * @Description 随机生成匿名用户id
	 * @return
	 */
	public String getAnonymousid() {
		String anno_id = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmss");
		anno_id = sdf.format(new Date()) + CommonUtils.getRandomString(3);
		return anno_id;
	}

}
